#include <htc.h>
#include"i2c.h"
#include"SPI.h"
#include"uart.h"
#include"ADC.h"

__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF);  //1st config. Word
__CONFIG(BOR4V_BOR21V); //2st config. Word 
#define _XTAL_FREQ 	4000000 
#define ADDRESS_SL 0x68

unsigned char time[7];
unsigned char set_time[7]={0x00,0x30,0x18,0x02,0x04,0x06,0x12};
unsigned char adc;
char s,*ch_s=&s;

void write_time()
{
	unsigned char i;
	i2c_start();
	i2c_write(0xd0);	//i2c_write((ADDRESS<<1)|0x00)
	i2c_write(0x00);	//register address
	for(i=0;i<7;i++)
		i2c_write(set_time[i]);	
	i2c_stop();
}

void read_time()
{
	unsigned char i;
	i2c_start();
	i2c_write(0xd0);
	i2c_write(0x00);
	i2c_stop();
	i2c_start();
	i2c_write(0xd1);
	for(i=0;i<6;i++)
		time[i]=i2c_read(0);
	time[6]=i2c_read(1);
	i2c_stop();
}

//----------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------

void port_init()
{
	//ADC
	ANSEL = 0x01;   // Chon kenh ADC AN1
	ANSELH = 0;		//

	
	TRISB = 0xff;
	WPUB = 0x00;
	PORTB = 0xff;
	IOCB = 0x0f;


	TRISA1 = 1;     // RA1 phai la Input de doc ADC
	//OUTPUT LED
	TRISD = 0;		// PORTD: output 
	PORTD = 0xff;
	
	TRISA2=0;	//***************************************************
	RA2=1;		//***************************************************
	// I2C
	RC3 = 1;
	RC4 = 1;
	TRISC3 = 1;
	TRISC4 = 1; //	control by mssp
	//SPI
	TRISE0 = 0;	// SCK
	TRISE1 = 0;	// SDO
	TRISE2 = 1;	// SDI
	TRISA5 = 0; // Slave Select
};
//----------------------------------------------------------------

//----------------------------------------------------------------
//Interrupt service rountine
void interrupt isr()
{

	if (RBIE && RBIF)
	{
		PORTD = 0x0f;
		__delay_ms(500);
		RBIF = 0;
		
	}

	int ADC_result;
	if (PEIE && ADIE && ADIF)
	{
		ADIF = 0;    //Clear ADC Interrupt Flag
		//Read ADC result
		ADC_result = (ADRESH<<8)|ADRESL;
		adc = ADC_result/100;
	}

 	if(RCIE &&RCIF) 
    {
	s=uart_getc();
	if(s=='t')
	{
		int i;
		if(uart_getc()=='i')
		{
			for(i=0;i<7;i++)
				set_time[i]=(uart_getc()-48)<<4|(uart_getc()-48);
			write_time();
		}
	}

	if(s=='p')
	{
		if(uart_getc()=='o')
		{
			s=(uart_getc()-48)<<4|(uart_getc()-48);
			PORTD=s;
		}
	}
	}


}
//----------------------------------------------------------------

void main()
{
	unsigned char spi_res, tam,i;
	port_init();
	intADC_init();
	adc_init();
  	i2c_init();
	spi_init();
	uart_init();    
    RBIF=0;
	RBIE=1;
	RCIF=0;
	RCIE=1;
	__delay_ms(10);
	//write_time();
	while(1)
	{
		read_time();
		spi_res = spi_read();
		//PORTD=spi_res;


		if(tam!=time[0])
		{
			RA2^=1;
			tam=time[0];
		}
	GO_nDONE  = 1; 	//ADC

	uart_putc((spi_res)/10+48);
	uart_putc((spi_res)%10+48);
	uart_puts("temp");
	for (i=0; i<7; i++)
	{
		uart_putc((time[i]>>4)+48);
		uart_putc((time[i]&0x0f)+48);
	}
	uart_puts("time");
	uart_putc(adc);
	uart_puts("adc ");
	__delay_ms(100);
	}
}

